//
//  WeatherTableViewCellOne.m
//  ToWhere
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "WeatherTableViewCellOne.h"
#import "WeatherViewController.h"
@implementation WeatherTableViewCellOne

- (void)dealloc
{
    [_weather release];
    [_qualityOfWeather release];
    [_qualityImage release];
    [_qualityLabel release];
    [_warningOfWeather release];
    [_warningImage release];
    [_warningLabel release];
    [_currentOfWeather release];
    [_currentTem release];
    [_garden release];
    [_sunrise release];
    [_sunriselabel release];
    [_sunset release];
    [_sunsetlabel release];
    [_date release];
    [_conference release];
    [_now release];
    [_labelNow release];
    [_weatherNow release];
    [_imageNow release];
    [_Tomorrow release];
    [_labelTomorrow release];
    [_weatherTomorrow release];
    [_imageTomorrow release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.qualityOfWeather = [UIButton buttonWithType:UIButtonTypeCustom];
        _qualityOfWeather.layer.masksToBounds = YES;
        _qualityOfWeather.layer.cornerRadius = 10;
        _qualityOfWeather.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.5];
        [self.contentView addSubview:_qualityOfWeather];
        [_qualityOfWeather release];
        
        self.qualityImage = [[UIImageView alloc] init];
        [self.contentView addSubview:_qualityImage];
        _qualityImage.layer.masksToBounds = YES;
        _qualityImage.layer.cornerRadius = 15;
        [_qualityImage release];
        
        self.qualityLabel = [[UILabel alloc] init];
        _qualityLabel.font = [UIFont systemFontOfSize:15 *kWidth];
        _qualityLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_qualityLabel];
        [_qualityLabel release];
        
        self.currentTem = [UIButton buttonWithType:UIButtonTypeCustom];
        self.currentTem.titleLabel.font = [UIFont systemFontOfSize:60 *kWidth];
        [self.currentTem addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_currentTem];
        [_currentTem release];
        
        self.garden = [[UIImageView alloc] init];
        [self.contentView addSubview:_garden];
        [_garden release];
        
        self.currentOfWeather = [[UILabel alloc] init];
        self.currentOfWeather.font = [UIFont systemFontOfSize:17 *kWidth];
        self.currentOfWeather.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_currentOfWeather];
        [_currentOfWeather release];
        
        self.sunrise = [[UIImageView alloc] init];
        [self.contentView addSubview:_sunrise];
        [_sunrise release];
        
        self.sunset = [[UIImageView alloc] init];
        [self.contentView addSubview:_sunset];
        [_sunset release];
        
        self.sunriselabel = [[UILabel alloc] init];
        self.sunriselabel.font = [UIFont systemFontOfSize:17 *kWidth];
        self.sunriselabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_sunriselabel];
        [_sunriselabel release];
        
        self.sunsetlabel = [[UILabel alloc] init];
        self.sunsetlabel.font = [UIFont systemFontOfSize:17 *kWidth];
        self.sunsetlabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_sunsetlabel];
        [_sunsetlabel release];
        
        self.date = [[UILabel alloc] init];
        self.date.font = [UIFont systemFontOfSize:17 *kWidth];
        self.date.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_date];
        [_date release];
        
        self.conference = [[UILabel alloc] init];
        self.conference.font = [UIFont systemFontOfSize:17 *kWidth];
        self.conference.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_conference];
        [_conference release];
        
        self.now = [[UILabel alloc] init];
        self.now.layer.borderWidth = 0.5;
        self.now.layer.borderColor = [UIColor whiteColor].CGColor;
        [self.contentView addSubview:_now];
        
        self.labelNow = [[UILabel alloc] init];
        self.labelNow.textColor = [UIColor whiteColor];
        self.labelNow.font = [UIFont systemFontOfSize:17 *kWidth];
        [self.contentView addSubview:_labelNow];
        [_labelNow release];
        
        self.imageNow = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageNow];
        [_imageNow release];
        
        self.weatherNow = [[UILabel alloc] init];
        self.weatherNow.font = [UIFont systemFontOfSize:17 *kWidth];
        self.weatherNow.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_weatherNow];
        [_weatherNow release];
        [_now release];
        
        self.Tomorrow = [[UILabel alloc] init];
        self.Tomorrow.layer.borderColor = [UIColor whiteColor].CGColor;
        self.Tomorrow.layer.borderWidth = 0.5;
        [self.contentView addSubview:_Tomorrow];
       
        self.weatherTomorrow = [[UILabel alloc] init];
        self.weatherTomorrow.font = [UIFont systemFontOfSize:17 *kWidth];
        self.weatherTomorrow.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_weatherTomorrow];
        [_weatherTomorrow release];
        
        self.imageTomorrow = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageTomorrow];
        [_imageTomorrow release];
        
        self.labelTomorrow = [[UILabel alloc] init];
        self.labelTomorrow.font = [UIFont systemFontOfSize:17 *kWidth];
        self.labelTomorrow.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_labelTomorrow];
        [_labelTomorrow release];
        [_Tomorrow release];
        
    }
    return self;
}
- (void)layoutSubviews{

    [super layoutSubviews];
    
    self.qualityOfWeather.frame = CGRectMake(15 *kWidth, 17 *kHeight, 140 *kWidth, 20 *kHeight);
   
    self.qualityImage.frame = CGRectMake(15 *kWidth, 13 *kHeight, 30 *kWidth, 30 *kWidth);
    
    self.qualityLabel.frame = CGRectMake(48 *kWidth, 17 *kHeight, 110 *kWidth, 20 *kHeight);
    
    self.warningOfWeather.frame = CGRectMake(15 *kWidth, 55 *kHeight, 150 *kWidth, 20 *kHeight);
    
    self.warningImage.frame = CGRectMake(15 *kWidth, 51 *kHeight, 30 *kWidth, 30 *kWidth);
    
    self.warningLabel.frame = CGRectMake(50 *kWidth, 55 *kHeight, 110 *kWidth, 20 *kHeight);
    ;
    
    self.currentTem.frame = CGRectMake(10 *kWidth, 350 *kHeight, 100 *kWidth, 100 *kHeight);
    
    self.garden.frame = CGRectMake(115 *kWidth, 350 *kHeight, 20 *kWidth, 20 *kHeight);
    
    self.currentOfWeather.frame = CGRectMake(130 *kWidth, 410 *kHeight, 150 *kWidth, 30 *kHeight);
    
    self.sunrise.frame = CGRectMake(10 *kWidth, 456 *kHeight, 30 *kWidth, 30 *kHeight);
    
    self.sunriselabel.frame = CGRectMake(50 *kWidth, 460 *kHeight, 100 *kWidth, 20 *kHeight);
    
    self.date.frame = CGRectMake(260 *kWidth, 460 *kHeight, 100 *kWidth, 20 *kHeight);
    
    self.sunset.frame = CGRectMake(10 *kWidth, 481 *kHeight, 30 *kWidth, 30 *kHeight);
    
    self.conference.frame = CGRectMake(255 *kWidth, 490*kHeight, 120 *kWidth, 20 *kHeight);
    
    self.sunsetlabel.frame = CGRectMake(50 *kWidth, 490 *kHeight, 100 *kWidth, 20 *kHeight);
    
    self.now.frame = CGRectMake(0, 530 *kHeight, self.frame.size.width / 2,60 *kHeight);
    
    self.labelNow.frame = CGRectMake(10 *kWidth, 532 *kHeight, 40 *kWidth, 25 *kHeight);
    
    self.weatherNow.frame = CGRectMake(10 *kWidth, 563 *kHeight, 80 *kWidth, 20 *kHeight);
    
    self.imageNow.frame = CGRectMake(150 *kWidth, 550 *kHeight, 30 *kWidth, 30 *kHeight);
    
    self.Tomorrow.frame = CGRectMake(self.frame.size.width / 2, 530 *kHeight, self.frame.size.width / 2,60 *kHeight);
    
    self.labelTomorrow.frame = CGRectMake(self.frame.size.width / 2 + 10 *kWidth, 535 *kHeight, 40 *kWidth, 25 *kHeight);
    
    self.weatherTomorrow.frame = CGRectMake(self.frame.size.width / 2 + 10 *kWidth, 560 *kHeight, 80 *kWidth, 20 *kHeight);
    
    self.imageTomorrow.frame = CGRectMake(self.frame.size.width / 2 + 150 *kWidth, 550 *kHeight, 30 *kWidth, 30 *kHeight);
  
   
}
- (void)setWeather:(WeatherModel *)weather{

    if (_weather != weather) {
        [_weather release];
        _weather = [weather retain];
    }
 
    self.qualityLabel.text = [NSString stringWithFormat:@"%@ %@",[[weather.pm objectForKey:@"gov"] objectForKey:@"airgrd"],[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"]];
    
    if ([[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"] containsString:@"优"]) {
        self.qualityImage.image = [UIImage imageNamed:@"weather_植物"];
        self.qualityImage.backgroundColor = [UIColor greenColor];
    }
    else if ([[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"] containsString:@"良"]) {
        self.qualityImage.image = [UIImage imageNamed:@"weather_植物"];
        self.qualityImage.backgroundColor = [UIColor yellowColor];
    }else if ([[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"] containsString:@"轻度污染"]) {
        self.qualityImage.image = [UIImage imageNamed:@"weather_植物"];
        self.qualityImage.backgroundColor = [UIColor orangeColor];
    }else if ([[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"] containsString:@"中度污染"]) {
        self.qualityImage.image = [UIImage imageNamed:@"weather_植物"];
        self.qualityImage.backgroundColor = [UIColor blueColor];
    }else if ([[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"] containsString:@"重度污染"]) {
        self.qualityImage.image = [UIImage imageNamed:@"weather_aqi_level_5@2x"];
        self.qualityImage.backgroundColor = [UIColor purpleColor];
    }
    else if ([[[weather.pm objectForKey:@"gov"] objectForKey:@"hint"] containsString:@"严重污染"]) {
        self.qualityImage.image = [UIImage imageNamed:@"weather_aqi_level_6@2x"];
        self.qualityImage.backgroundColor = [UIColor redColor];
    }
    
    self.garden.image = [UIImage imageNamed:@"Weather_du2"];
    [self.currentTem setTitle:[weather.now objectForKey:@"temp"] forState:UIControlStateNormal];
    self.currentOfWeather.text = [weather.now objectForKey:@"nowweather"];
    
    self.sunriselabel.text = [weather.sun objectForKey:@"sunrise"];
    self.sunsetlabel.text = [weather.sun objectForKey:@"sunset"];
    self.date.text = [[weather.sun objectForKey:@"sysdate"] substringWithRange:NSMakeRange(0, 10)];
    self.conference.text = [NSString stringWithFormat:@"%@ 发布",[[weather.sun objectForKey:@"sysdate"] substringFromIndex:10]];
    self.labelNow.text = @"今日";
    self.labelTomorrow.text = @"明天";
    self.sunrise.image = [UIImage imageNamed:@"weather_sunrise@2x"];
    self.sunset.image = [UIImage imageNamed:@"weather_sunset@2x"];
    
    self.weatherNow.text = [NSString stringWithFormat:@"%@/%@℃",[[weather.weatherinfo objectForKey:@"weather"][1] objectForKey:@"hightemp"],[[weather.weatherinfo objectForKey:@"weather"][1] objectForKey:@"lowtemp"] ];
    self.weatherTomorrow.text = [NSString stringWithFormat:@"%@/%@℃",[[weather.weatherinfo objectForKey:@"weather"][2] objectForKey:@"hightemp"],[[weather.weatherinfo objectForKey:@"weather"][2] objectForKey:@"lowtemp"] ];
    NSString *imageName = @"";
    if ([[weather.now objectForKey:@"nowweather"] containsString:@"阴"]) {
        imageName = @"weathericon_3阴@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"晴"]){
        imageName = @"weathericon_1晴日@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"多云"]){
        
        imageName = @"weathericon_2多云日@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"霾"]){
    
        imageName = @"weathericon_6霾@2x";
    
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"小雨"]){
        imageName = @"weathericon_7小雨@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"中雨"]){
    
        imageName = @"weathericon_8中雨@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"大雨"]){
    
        imageName = @"weathericon_9大雨@2x";
    }else if([[weather.now objectForKey:@"nowweather"] containsString:@"阵雨"]){
        imageName = @"weathericon_13阵雨日@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"冰雹"]){
    
        imageName = @"weathericon_17冰雹@2x";
    
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"雷"]){
    
        imageName = @"weathericon_15雷电@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"雨夹雪"]){
        
        
        imageName = @"weathericon_18雨夹雪@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"小雪"]){
        
        imageName = @"weathericon_19小雪@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"中雪"]){
        
        imageName = @"weathericon_20中雪@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"大雪"]){
        
        imageName = @"weathericon_21大雪@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"暴雪"]){
        
        imageName = @"weathericon_22暴雪@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"阵雪"]){
        imageName = @"weathericon_23阵雪@2x";
    }
    else if ([[weather.now objectForKey:@"nowweather"] containsString:@"风"]){
        
        imageName = @"weathericon_37大风@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"台风"]){
        
        imageName = @"weathericon_38台风@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"龙卷风"]){
        
        imageName = @"weathericon_40龙卷风@2x";
    }else if ([[weather.now objectForKey:@"nowweather"] containsString:@"雾"]){
        
        imageName = @"weathericon_4雾@2x";
    }
    
    self.imageNow.image = [UIImage imageNamed:imageName];
    
    NSString *imageNameTo = nil;
    if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"]containsString:@"阴"]) {
        imageNameTo = @"weathericon_3阴@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"晴"]){
        imageNameTo = @"weathericon_1晴日@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"多云"]){
        
        imageNameTo = @"weathericon_2多云日@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"霾"]){
        
        imageNameTo = @"weathericon_6霾@2x";
        
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"小雨"]){
        imageNameTo = @"weathericon_7小雨@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"中雨"]){
        
        imageNameTo = @"weathericon_8中雨@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"大雨"]){
        
        imageNameTo = @"weathericon_9大雨@2x";
    }else if([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"阵雨"]){
        imageNameTo = @"weathericon_13阵雨日@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"冰雹"]){
        
        imageNameTo = @"weathericon_17冰雹@2x";
        
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"雷"]){
        
        imageNameTo = @"weathericon_15雷电@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"雨夹雪"]){
        
        imageNameTo = @"weathericon_18雨夹雪@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"小雪"]){
        
        imageNameTo = @"weathericon_19小雪@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"中雪"]){
        
        imageNameTo = @"weathericon_20中雪@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"大雪"]){
        
        imageNameTo = @"weathericon_21大雪@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"暴雪"]){
        
        imageNameTo = @"weathericon_22暴雪@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"阵雪"]){
        imageNameTo = @"weathericon_23阵雪@2x";
    }
    else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"风"]){
        
        imageNameTo = @"weathericon_37大风@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"台风"]){
        
        imageNameTo = @"weathericon_38台风@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"龙卷风"]){
        
        imageNameTo = @"weathericon_40龙卷风@2x";
    }else if ([[[weather.weatherinfo objectForKey:@"weather"] [2] objectForKey:@"dayweather"] containsString:@"雾"]){
        
        imageNameTo = @"weathericon_4雾@2x";
    }

    self.imageTomorrow.image = [UIImage imageNamed:imageNameTo];
    
    
    
}
+ (WeatherTableViewCellOne *)weatherTableViewCellOneWithTableView:(UITableView *)tableView{

    static NSString *weatherTableViewCellOne = @"WeatherTableViewCellOne";
    WeatherTableViewCellOne *cell = [tableView dequeueReusableCellWithIdentifier:weatherTableViewCellOne];
    if (!cell) {
        cell = [[WeatherTableViewCellOne alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:weatherTableViewCellOne];
    }
    return cell;
}
- (void)push{
    if (self.weather == nil) {
        return;
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObject:self.weather forKey:@"loop"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"push" object:self userInfo:dic];
}
@end
